home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / simula / books / books.lha / kirkerud / studset2.sim < prev    next >
Text File  |  1993-08-16  |  13KB  |  336 lines

  1. % ****************************************************************
  2. % *                                                              *
  3. % *  This is a revised version the program constructed in        *
  4. % *  example 14.1 of          *
  5. % *  Object Oriented Programming with Simula by Bj|rn Kirkerud;  *
  6. % *  The revisions in the program consist in the use of          *
  7. % *  a few classes containing generally useful tools             * 
  8. % *                                                              *
  9. % ****************************************************************
  10.  
  11. external class Settools;
  12.  
  13. Settools begin
  14.  
  15. % ****************************************************************
  16. % *                                                              *
  17. % *  Declarations of classes containing generally usefull tools: *
  18. % *                                                              *
  19. % ****************************************************************
  20.  
  21.   external class promptools, texttools, filetools;
  22.  
  23.   ref(promptools) pt;
  24.   ref(texttools)  tt;
  25.   ref(filetools)  ft;
  26.  
  27.   procedure outline(line); text line;
  28.     begin outtext(line); outimage end;
  29.  
  30.  
  31.  
  32. % ****************************************************************
  33. % *                                                              *
  34. % *  The class Student:                                          *
  35. % *                                                              *
  36. % ****************************************************************
  37.  
  38.   element class Student(ident); text ident;
  39.     begin
  40.  
  41.     ! Variables to keep the data that describe this student:   ;
  42.  
  43.       integer year, month, day, form;
  44.       Boolean female;
  45.       character math_grade, eng_grade, hist_grade;
  46.  
  47.  
  48.     ! A procedure with a value which is unique for this student:  ;
  49.  
  50.       text procedure key; key :- ident;
  51.  
  52.  
  53.     ! A variable to hold a reference to the next Student in a pointer chain:   ;
  54.  
  55.       ref(Student) next_in_chain;
  56.  
  57.  
  58.     ! Data access procedures:   ;
  59.  
  60.       procedure read;
  61.         begin
  62.           year          := pt.ask_for_int("Year of birth? ");
  63.           month      := pt.ask_for_int("Month? ");
  64.           day          := pt.ask_for_int("Day? ");
  65.           form          := pt.ask_for_int("Form? ");
  66.           female     := pt.ask_for_bool("Female? ");
  67.           math_grade := pt.ask_for_char("Grade in mathematics? ");
  68.           eng_grade  := pt.ask_for_char("Grade in English? ");
  69.           hist_grade := pt.ask_for_char("Grade in history? ");
  70.         end of Student'read;
  71.  
  72.       procedure display;
  73.         begin
  74.           outtext("Data for student " & ident);
  75.           outtext(".  Born: ");    
  76.              outint(day,   2); outchar('/');
  77.              outint(month, 2); outchar('/');
  78.              outint(year,  4);
  79.           outline(". " & (if female then "Female"  else "Male") & ".  ");
  80.           outline("Form: " & tt.int_as_text(form) & ".  " &
  81.                   "Current grades: " &
  82.                   "Mathematics: "    & tt.char_as_text(math_grade) & ", " &
  83.                   "English: "        & tt.char_as_text(eng_grade)  & ", " &
  84.                   "History: "        & tt.char_as_text(hist_grade) & ".");
  85.         end of Student'display;
  86.  
  87.       procedure change;
  88.         begin character attribute;
  89.           attribute := pt.ask_for_char("What do you want  to change? ");
  90.           if attribute = 'y' then        
  91.              year       := pt.ask_for_int("New birth year? ")         else
  92.           if attribute = 'm' then       
  93.              month      := pt.ask_for_int("New birth month? ")       else
  94.           if attribute = 'd' then         
  95.              day        := pt.ask_for_int("New day of birth? ")      else
  96.           if attribute = 'f' then        
  97.              form       := pt.ask_for_int("New form number? ")       else
  98.           if attribute = 's' then      
  99.              female     := pt.ask_for_bool("Female? ")               else
  100.           if attribute = 'a' then  
  101.              math_grade := pt.ask_for_char("New grade in math? ")    else
  102.           if attribute = 'e' then   
  103.              eng_grade  := pt.ask_for_char("New grade in English? ") else
  104.           if attribute = 'h' then  
  105.              hist_grade := pt.ask_for_char("New grade in history? ")
  106.         else begin
  107.               outline("You can change one of the  following attributes:");
  108.               outline("  m: Birth month");    
  109.               outline("  d: Day of birth");
  110.               outline("  f: Form number");    
  111.               outline("  s: Sex");
  112.               outline("  a: Grade in mathematics");
  113.               outline("  e: Grade in English");
  114.               outline("  h: Grade in history");
  115.               change; 
  116.             end;
  117.         end of Student'change;
  118.  
  119.       character procedure worst_grade;
  120.         worst_grade := max(math_grade, max(eng_grade,  hist_grade));
  121.  
  122.       procedure put_in_record(outf); ref(outfile) outf;
  123.         begin
  124.           outf.outtext(ident); outf.setpos(11);
  125.           ft.int_to_file(outf, year); 
  126.           ft.int_to_file(outf, month); 
  127.           ft.int_to_file(outf, day); 
  128.           ft.int_to_file(outf, form); 
  129.           ft.bool_to_file(outf, female);
  130.           outf.outchar(math_grade); 
  131.           outf.outchar(eng_grade); 
  132.           outf.outchar(hist_grade); 
  133.         end;
  134.  
  135.       procedure get_from_record(inf); ref(infile) inf;
  136.         begin
  137.           year       := ft.int_from_file(inf); 
  138.           month      := ft.int_from_file(inf); 
  139.           day        := ft.int_from_file(inf); 
  140.           form       := ft.int_from_file(inf); 
  141.           female     := ft.bool_from_file(inf); 
  142.           math_grade := inf.inchar; 
  143.           eng_grade  := inf.inchar; 
  144.           hist_grade := inf.inchar; 
  145.         end;
  146.  
  147.     end of Student;
  148.  
  149.  
  150. % ****************************************************************
  151. % *                                                              *
  152. % *  Declaration of a variable to keep a reference to            *
  153. % *  an ordered set of Students:                                 *
  154. % *                                                              *
  155. % ****************************************************************
  156.  
  157.   ref(ordered_set) The_students;
  158.  
  159.  
  160. % ****************************************************************
  161. % *                                                              *
  162. % *  Declarations of command procedures:                         *
  163. % *                                                              *
  164. % ****************************************************************
  165.  
  166.   procedure Give_help;
  167.     begin
  168.       outline("The legal commands are: "); 
  169.       outline("   ?:  Help (writes this text)"); 
  170.       outline("   N:  To enter data about a new student"); 
  171.       outline("   W:  Writes data about a specified student"); 
  172.       outline("   L:  Writes a list with all students"); 
  173.       outline("   C:  Changes data about a specified student"); 
  174.       outline("   R:  Removes all data about a specified student"); 
  175.       outline("   P:  Puts all data to file ""stud.dta"""); 
  176.       outline("   G:  Gets data from file ""stud.dta"""); 
  177.       outline("   B:  Writes students with bad grades"); 
  178.       outline("   Q:  Quit (the program execution stops)"); 
  179.     end of Give_help;
  180.  
  181.   procedure Enter_student;
  182.     begin  ref(Student) a_student; Boolean ident_exists;
  183.       a_student :- new Student(pt.ask_for_text("Identity number? "));
  184.       The_students.add_element(a_student, ident_exists);
  185.       if ident_exists
  186.         then outline("The identity number " & a_student.key &
  187.                      " is already  in use!")
  188.         else begin
  189.             a_student.read;
  190.             outline("The data for student " & a_student.key &
  191.                     " have been stored.");
  192.           end;
  193.     end of Enter_student;
  194.  
  195.   procedure Write_student;
  196.     begin ref(Student) a_student;
  197.       a_student :- The_students.find_element(pt.ask_for_text("Id. number? "));
  198.       if a_student == none
  199.         then outline("No student with that identity number!")
  200.         else a_student.display;
  201.     end of Write_student;
  202.  
  203.   procedure List_students;
  204.     if The_students.is_empty 
  205.       then outline("Data for no students have been entered")
  206.       else begin ref(Student) a_student;
  207.         outline("Data for " & tt.int_as_text(The_students.size) &
  208.                 " students have been entered. " & 
  209.                 " These data are as follows:");
  210.         a_student :- The_students.first_element;
  211.         while a_student =/= none do
  212.           begin
  213.             a_student.display;
  214.             a_student :- The_students.next_element;
  215.           end;
  216.       end of List_students;
  217.  
  218.   procedure Change_student;
  219.     begin ref(Student) a_student; 
  220.       a_student :- The_students.find_element(pt.ask_for_text("Id. number? "));
  221.       if a_student == none
  222.         then outline("No student with that identity number!")
  223.         else begin a_student.display;  a_student.change end;
  224.     end of Change_student;
  225.  
  226.   procedure Remove_student;
  227.     begin integer ident_number;  Boolean no_such_student;
  228.       ident_number := pt.ask_for_int("Identity number? ");
  229.       The_students.Remove_element(pt.ask_for_text("Id. number? "),  
  230.                                   no_such_student);
  231.       if no_such_student
  232.         then outline("No student with that identity  number!")
  233.         else outline("The student has been removed!");
  234.     end of Remove_student;
  235.  
  236.   procedure Put_to_file;
  237.     inspect new outfile(pt.ask_for_text("Write name of outfile> ")) do
  238.       begin ref(Student) a_student;
  239.         open(blanks(28));
  240.         a_student :- The_students.first_element;
  241.         while a_student =/= none do
  242.           begin
  243.             a_student.put_in_record(this outfile);
  244.             outimage;
  245.             a_student :- The_students.next_element;
  246.           end;
  247.         close;
  248.       end;
  249.  
  250.   procedure Get_from_file;
  251.     inspect new infile(pt.ask_for_text("Write name of infile> ")) do
  252.       if open(blanks(28)) 
  253.         then begin ref(Student) a_student;
  254.           inimage;
  255.           while not endfile do
  256.             begin
  257.               a_student :- new Student(intext(10).strip);
  258.               a_student.get_from_record(this infile);
  259.               The_students.add_element_ok(a_student);
  260.               inimage;
  261.             end;
  262.           close;
  263.         end
  264.       else outline("File " & filename & " can not be opened for reading");
  265.  
  266.   procedure Bad_grades;
  267.     begin character grade_limit;  ref(Student) a_student;
  268.       grade_limit := pt.ask_for_char("Grade limit? ");
  269.       a_student :- The_students.first_element;
  270.       while a_student =/= none do
  271.         begin
  272.           if a_student.worst_grade ge grade_limit then a_student.display;
  273.           a_student :- The_students.next_element;
  274.         end;
  275.     end of Bad_grades;
  276.  
  277.  
  278.   procedure Unknown_command(c); character c;
  279.     begin
  280.       outline("   You gave the command '" & tt.char_as_text(c) & "'" &
  281.               "   This is not among the legal commands.");
  282.       outline("   Type ? if you don't remember the legal commands"); 
  283.     end of Unknown_command;
  284.  
  285.  
  286. % ****************************************************************
  287. % *                                                              *
  288. % *  Declaration of a variable to keep the latest command        *
  289. % *  typed by the user:                                          *
  290. % *                                                              *
  291. % ****************************************************************
  292.  
  293.   character command;
  294.  
  295.  
  296. % ****************************************************************
  297. % * St*
  298. % *  That was the last declaration.                              *
  299. % *  Now come the imperatives of the program:                    *
  300. % *        S                  *
  301. % ****************************************************************
  302.  
  303. % *  First, initialization of the data-structure:  ;
  304.  
  305.   The_students :- new ordered_set;
  306.  
  307.  
  308. % *  then, initialization of tool-boxes:  ;
  309.  
  310.   pt :- new promptools;
  311.   tt :- new texttools;
  312.   ft :- new filetools;
  313.  
  314.  
  315. % *  Finally, read and execute commands typed by the user:  ;
  316.  
  317.   command := pt.ask_for_char("Type your first command  (? for help) > ");
  318.   while command ne 'Q' do
  319.     begin
  320.       if command = '?' then Give_help         else
  321.       if command = 'N' then Enter_student  else
  322.       if command = 'W' then Write_student  else
  323.       if command = 'L' then List_students  else
  324.       if command = 'C' then Change_student else
  325.       if command = 'R' then Remove_student else
  326.       if command = 'P' then Put_to_file    else
  327.       if command = 'G' then Get_from_file  else
  328.       if command = 'B' then Bad_grades
  329.       else Unknown_command(command);
  330.       command := pt.ask_for_char("Your next command > ");
  331.     end;
  332.  
  333.   outline("Bye");
  334.  
  335. end
  336.